package com.shensi.adminCollect.controller.coupon;

import com.shensi.common.core.controller.BaseController;
import com.shensi.common.core.domain.AjaxResult;
import com.shensi.common.core.page.TableDataInfo;
import com.shensi.adminCollect.common.utils.poi.ExcelUtil;
import com.shensi.adminCollect.domain.coupon.CouponUser;
import com.shensi.adminCollect.domain.coupon.vo.CouponUserSumVo;
import com.shensi.adminCollect.service.coupon.ICouponUserService;
import com.shensi.adminCollect.domain.system.SysAppUserInfoUser;
import com.shensi.adminCollect.service.system.IAppUserInfoUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;

import java.util.List;

/**
 * 优惠劵Controller
 *
 * @author qujia
 * @date 2021-08-09
 */
@RestController
@RequestMapping("/coupon/user")
@Api(tags = "优惠券用户相关接口", description = "优惠券用户相关接口")
public class CouponUserController extends BaseController
{
    private static final Logger log = LoggerFactory.getLogger(CouponUser.class);
    @Autowired
    private ICouponUserService couponUserService;
    @Autowired
    private IAppUserInfoUserService appUserInfoUserService;


    /**
     * 查询人员优惠劵发放方式列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "优惠券活动统计列表", notes = "优惠券活动统计列表")
    public TableDataInfo list(CouponUser couponUser)
    {
        LoginUser current= SecurityUtils.getLoginUser();
        couponUser.setEnterpriseId(current.getUser().getEnterpriseId());
        startPage();
        List<CouponUser> couponUserlist = couponUserService.selectCouponUserList(couponUser);
        if(couponUserlist!=null&&couponUserlist.size()>0) {
            for (CouponUser couponUser1 : couponUserlist) {
                SysAppUserInfoUser sysAppUserInfoUser = new SysAppUserInfoUser();
                sysAppUserInfoUser.setUnionId(couponUser1.getUnionId());
                sysAppUserInfoUser.setMobilePhone(couponUser1.getMobilePhone());
                sysAppUserInfoUser.setStationId(current.getUser().getEnterpriseId());
                List<SysAppUserInfoUser> sysAppUserInfoUsers = appUserInfoUserService.selectAppUserInfoList(sysAppUserInfoUser);
                if (sysAppUserInfoUsers != null && sysAppUserInfoUsers.size() > 0) {
                    couponUser1.setBlogNickName(sysAppUserInfoUsers.get(0).getBlogNickName());
                }

            }
        }
        return getDataTable(couponUserlist);
    }


    /**
     * 手机号查优惠券列表
     */
    @GetMapping("/listByPhone")
    @ApiOperation(value = "手机号查优惠券列表", notes = "手机号查优惠券列表")
    public TableDataInfo listByPhone(CouponUser couponUser)
    {
        LoginUser current= SecurityUtils.getLoginUser();
        couponUser.setEnterpriseId(current.getUser().getEnterpriseId());
        startPage();
        List<CouponUser> couponUserlist = couponUserService.selectCouponUserList(couponUser);
        if(couponUserlist!=null&&couponUserlist.size()>0) {
            for (CouponUser couponUser1 : couponUserlist) {
                SysAppUserInfoUser sysAppUserInfoUser = new SysAppUserInfoUser();
                sysAppUserInfoUser.setUnionId(couponUser1.getUnionId());
                sysAppUserInfoUser.setMobilePhone(couponUser1.getMobilePhone());
                sysAppUserInfoUser.setStationId(current.getUser().getEnterpriseId());
                List<SysAppUserInfoUser> sysAppUserInfoUsers = appUserInfoUserService.selectAppUserInfoList(sysAppUserInfoUser);
                if (sysAppUserInfoUsers != null && sysAppUserInfoUsers.size() > 0) {
                    couponUser1.setBlogNickName(sysAppUserInfoUsers.get(0).getBlogNickName());
                }

            }
        }
        return getDataTable(couponUserlist);
    }


    /**
     * 查询人员优惠劵发放方式列表
     */
    @GetMapping("/getSumCouponUser")
    @ApiOperation(value = "优惠券活动统计汇总", notes = "优惠券活动统计汇总")
    public CouponUserSumVo getSumCouponUser(CouponUser couponUser)
    {
        LoginUser current= SecurityUtils.getLoginUser();
        couponUser.setEnterpriseId(current.getUser().getEnterpriseId());

        CouponUserSumVo couponUserSum = couponUserService.selectCouponUserSum(couponUser);

        return couponUserSum;
    }


    // 逻辑删除优惠券用户
    @GetMapping("/delete/{id}")
    @ApiOperation(value = "逻辑删除优惠券用户", notes = "根据用户ID逻辑删除优惠券用户")
    public ResponseEntity<String> logicDeleteCouponUser(@ApiParam(value = "ID", required = true) @PathVariable Integer id) {
        couponUserService.logicDeleteCouponUser(id);
        return ResponseEntity.ok("删除成功");
    }

    // 批量逻辑删除优惠券用户
    @PostMapping("/batchDelete")
    @ApiOperation(value = "批量逻辑删除优惠券用户", notes = "根据用户ID列表批量逻辑删除优惠券用户")
    public ResponseEntity<String> batchLogicDeleteCouponUser(@ApiParam(value = "ID列表", required = true) @RequestBody List<Integer> ids) {
        couponUserService.batchLogicDeleteCouponUser(ids);
        return ResponseEntity.ok("删除成功");
    }

    /**
     * 导出人员优惠劵发放方式列表
     */
    @GetMapping("/export")
    public AjaxResult export(CouponUser couponUser)
    {
        LoginUser current= SecurityUtils.getLoginUser();
        couponUser.setEnterpriseId(current.getUser().getEnterpriseId());
        List<CouponUser> list = couponUserService.selectCouponUserList(couponUser);
        ExcelUtil<CouponUser> util = new ExcelUtil<CouponUser>(CouponUser.class);
        return util.exportExcel(list, "user");
    }



    /**
     * 获取人员优惠劵发放方式详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        //查询人员优惠劵发放方式
        CouponUser couponUser = couponUserService.selectCouponUserById(id);
        return AjaxResult.success(couponUser);
    }

    /**
     * 新增人员优惠劵
     */
    @PostMapping
    @Transactional
    public AjaxResult add(@RequestBody CouponUser couponUser)
    {
        int i=0;
         i=couponUserService.insertCouponUser(couponUser);
        return toAjax(i);
    }
    /**
     * 修改优惠劵发放方式
     */
    @PutMapping
    @Transactional
    public AjaxResult edit(@RequestBody CouponUser couponUser)
    {
        int i=0;
        i=couponUserService.updateCouponUser(couponUser);
        return toAjax(i);
    }
    /**
     * 删除人员优惠劵发放方式
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(couponUserService.deleteCouponUserByIds(ids));
    }
}
